﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class cms_Admin_Denghi_u_Denghiden : System.Web.UI.UserControl
{
    #region[Khai bao]
    string p = "";
    string uID = "";
    static string adm = "";
    #endregion
    #region[Page_Load]
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uid"] != null) { uID = Session["uid"].ToString(); }
        if (Session["p"] != null) { p = Session["p"].ToString(); }
        if (Session["adm"] != null) { adm = Session["adm"].ToString(); }
        if (!IsPostBack) { ViewlistUser(); Viewlist(); }
    }
    #endregion
    #region[Viewlist]
    void Viewlist()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByAdmin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuRec(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistAsc]
    void ViewlistAsc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByAdmin_Asc();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuRec_Asc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistNameAsc]
    void ViewlistNameAsc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuRec_Name_Asc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuRec_Name_Asc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistNameDesc]
    void ViewlistNameDesc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuRec_Name_Desc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuRec_Name_Desc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistStatusAsc]
    void ViewlistStatusAsc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuRec_Status_Asc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuRec_Status_Asc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistStatusDesc]
    void ViewlistStatusDesc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuRec_Status_Desc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuRec_Status_Desc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistNguoinhanAsc]
    void ViewlistNguoinhanAsc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuSend_Nguoinhan_Asc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuSend_Nguoinhan_Asc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[ViewlistNguoinhanDesc]
    void ViewlistNguoinhanDesc()
    {
        List<tbProposalDATA> list = new List<tbProposalDATA>();
        if (adm == "1")
        {
            list = tbProposalDB.tbProposal_GetByuSend_Nguoinhan_Desc_Admin();
        }
        else
        {
            list = tbProposalDB.tbProposal_GetByuSend_Nguoinhan_Desc(uID);
        }
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[View, Delete]
    protected void rptDanhsach_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string ten = e.CommandName.ToString();
        Session["ID"] = e.CommandArgument.ToString();
        switch (ten)
        {
            case "Edit":
                List<tbProposalDATA> list = tbProposalDB.tbProposal_GetByID(Session["ID"].ToString());
                lblNguoinhan.Text = list[0].Nguoigui;
                lblTieude.Text = list[0].prName;
                lblIDnguoigui.Text = list[0].uSend;
                if (list[0].prStatus == "1") { lbtPhanhoi.Visible = false; lbtDuyet.Visible = false; } else { lbtPhanhoi.Visible = true; lbtDuyet.Visible = true; }
                List<tbUsersDATA> listu = tbUserDB.tbUsers_GetByID(list[0].uSend);
                if (listu.Count > 0)
                {
                    lblPhongban.Text = listu[0].dpName;
                    lblChucvu.Text = listu[0].psName;
                    lblTell.Text = listu[0].uTel;
                    lblEmail.Text = listu[0].uEmail;
                }
                listu.Clear();
                listu = null;
                List<tbReplyDATA> listre = tbReplyDB.tbReply_GetByIDprID(Session["ID"].ToString());
                rptSub.DataSource = listre;
                rptSub.DataBind();
                listre.Clear();
                listre = null;
                pnDanhsach.Visible = false;
                pnUpdate.Visible = true;
                pnPhanhoi.Visible = false;
                list.Clear();
                list = null;
                break;
            case "Duyet":
                List<tbProposalDATA> listd = tbProposalDB.tbProposal_GetByID(Session["ID"].ToString());
                if (listd.Count > 0)
                {
                    listd[0].prID = Session["ID"].ToString();
                    try
                    {
                        if (tbProposalDB.tbProposal_Update(listd[0]))
                        {
                            lblThongbao.Text = "Đề nghị đã được duyệt thành công !!";
                            Viewlist();
                        }
                    }
                    catch { lblLoi.Text = "Lỗi xử lý dữ liệu !!"; return; }
                }
                break;
        }
    }
    #endregion
    #region[Delete]
    protected void Delete(object sender, System.EventArgs e)
    {
        ((LinkButton)sender).Attributes["onclick"] = "return confirm('Thứ ngài! Ngày chắc chắn muốn duyệt đề nghị này ??') ";
    }
    #endregion
    #region[FormatDate]
    protected string FormatDate(object date)
    {
        if (date.ToString().Trim().Length > 0)
        {
            if (DateTime.Parse(date.ToString()).Year != 1900)
            {
                DateTime dNgay = Convert.ToDateTime(date.ToString());
                return ((DateTime)dNgay).ToString("dd/MM/yyyy hh:mm");
            }
            else
            {
                return "";
            }
        }
        else
        {
            return "";
        }
    }
    #endregion
    #region[ViewStatus]
    protected string Status(string status)
    {
        string Chuoi = "";
        if (status == "0") { Chuoi = "Chưa duyệt"; } else { Chuoi = "Đã duyệt"; }
        return Chuoi;
    }
    #endregion
    #region[Viewnguoinhan]
    protected string Viewnguoinhan(string sendid)
    {
        string Chuoi = "";
        string Chuoi1 = "<img src=\"images/admin/kt.gif\" style=\"border: 0px\"/>";
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetByNguoiGui_prID(sendid);
        if (list.Count > 0)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (i == 1)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 2)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 3)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 4)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 5)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 6)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 7)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 8)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 9)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else if (i == 10)
                {
                    Chuoi += "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"images/admin/sub.gif\" style=\"border: 0px\"/>" + list[i].uName;
                }
                else
                {
                    Chuoi = Chuoi1 + "<font style=\"color: #0033ff\">" + list[i].uName + "</font>";
                }
            }
        }
        list.Clear();
        list = null;
        return Chuoi;
    }
    #endregion
    #region[SortDesc]
    protected void lbtGiam_Click(object sender, EventArgs e)
    {
        Viewlist();
    }
    #endregion
    #region[SortAsc]
    protected void lbtTang_Click(object sender, EventArgs e)
    {
        ViewlistAsc();
    }
    #endregion
    #region[SortNameAsc]
    protected void ltrTieudetangdan_Click(object sender, EventArgs e)
    {
        ViewlistNameAsc();
    }
    #endregion
    #region[SortNameDesc]
    protected void lbtTieudegiamdan_Click(object sender, EventArgs e)
    {
        ViewlistNameDesc();
    }
    #endregion
    #region[SortStatusAsc]
    protected void lbtTinhtrangtang_Click(object sender, EventArgs e)
    {
        ViewlistStatusAsc();
    }
    #endregion
    #region[SortStatusDesc]
    protected void lbtTinhtranggiam_Click(object sender, EventArgs e)
    {
        ViewlistStatusDesc();
    }
    #endregion
    #region[SortNguoinhanAsc]
    protected void lbtNguoinhantang_Click(object sender, EventArgs e)
    {
        ViewlistNguoinhanAsc();
    }
    #endregion
    #region[SortNguoinhanGiam]
    protected void lbtNguoinhangiam_Click(object sender, EventArgs e)
    {
        ViewlistNguoinhanDesc();
    }
    #endregion
    #region[Back]
    protected void lbtTrolai_Click(object sender, EventArgs e)
    {
        pnDanhsach.Visible = true;
        pnUpdate.Visible = false;
        Session["ID"] = null;
        Viewlist();
    }
    #endregion
    #region[Phanhoi]
    protected void lbtPhanhoi_Click(object sender, EventArgs e)
    {
        pnPhanhoi.Visible = true;
        pnDanhsach.Visible = false;
    }
    #endregion
    #region[Resetcontrol]
    void Resetcontrol()
    {
        lblTieude.Text = "";
        lblPhongban.Text = "";
        lblChucvu.Text = "";
        lblTell.Text = "";
        lblEmail.Text = "";
        fckNoidung.Value = "";
    }
    #endregion
    #region[Capnhatphanhoi]
    protected void lbtGuiPhanhoi_Click(object sender, EventArgs e)
    {
        if (fckNoidung.Value == "" || fckNoidung.Value == "<br type=\"_moz\" />") { lblLoi.Text = "Vui lòng nhập nội dung phản hồi !!"; return; } else { lblLoi.Text = ""; }
        string img_path = "";
        if (fileupload.PostedFile != null && fileupload.PostedFile.ContentLength > 0)
        {
            img_path = System.IO.Path.GetFileName(fileupload.PostedFile.FileName);
            fileupload.PostedFile.SaveAs(Server.MapPath("Users/files/") + img_path.ToString().Trim());
            img_path = "Users/files/" + img_path.ToString().Trim();
        }
        tbReplyDATA ObjtbReplyDATA = new tbReplyDATA();
        ObjtbReplyDATA.prID = Session["ID"].ToString();
        ObjtbReplyDATA.reName = lblTieude.Text;
        ObjtbReplyDATA.reContent = fckNoidung.Value;
        ObjtbReplyDATA.peFiles = img_path;
        ObjtbReplyDATA.uSend = uID;
        ObjtbReplyDATA.uRec = lblIDnguoigui.Text;
        ObjtbReplyDATA.reType = "1";
        try
        {
            if (tbReplyDB.tbReply_Add(ObjtbReplyDATA))
            {
                lblThongbao.Text = "Phản hồi đề nghị thành công !!";
                Resetcontrol();
                pnDanhsach.Visible = true;
                pnUpdate.Visible = false;
                pnPhanhoi.Visible = false;
                lbtDuyet.Visible = false;
            }
        }
        catch { lblLoi.Text = "Lỗi dữ liệu nhập vào !!"; return; }
        Session["ID"] = null;
    }
    #endregion
    #region[Huy]
    protected void lbtHuy_Click(object sender, EventArgs e)
    {
        pnPhanhoi.Visible = false;
    }
    #endregion
    #region[ViewClass]
    protected string ViewClass(string dk)
    {
        string Chuoi = "";
        if (dk == "0")
        {
            Chuoi = "icoden";
        }
        else
        {
            Chuoi = "icodi";
        }
        return Chuoi;
    }
    #endregion
    #region[Duyet len cap tren]
    protected void lbtDuyet_Click(object sender, EventArgs e)
    {
        pnDuyet.Visible = true;
        pnDanhsach.Visible = false;
    }
    #endregion
    #region[AHClass]
    protected bool AHClass(string dk)
    {
        if (dk == "0")
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion
    #region[ViewlistUser]
    void ViewlistUser()
    {
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetByAll();
        drlNguoinhan.Items.Clear();
        drlNguoinhan.Items.Add(new ListItem("===Chọn người nhận===","0"));
        for (int i = 0; i < list.Count; i++)
        {
            drlNguoinhan.Items.Add(new ListItem(list[i].uName,list[i].uID));
        }
        list.Clear();
        list = null;
    }
    #endregion
    #region[Duyet]
    protected void lbtDuyet2_Click(object sender, EventArgs e)
    {
        List<tbProposalDATA> list = tbProposalDB.tbProposal_GetByID(Session["ID"].ToString());
        if (list.Count > 0)
        {
            list[0].prID = Session["ID"].ToString();
            try
            {
                if (tbProposalDB.tbProposal_Update(list[0]))
                {
                    lblThongbao.Text = "Đề nghị đã được duyệt thành công !!";
                    Resetcontrol();
                    Viewlist();
                    pnDanhsach.Visible = true;
                    pnUpdate.Visible = false;
                    lbtDuyet.Visible = false;
                }
            }
            catch { lblLoi.Text = "Lỗi xử lý dữ liệu !!"; return; }
        }
    }
    #endregion
    #region[Gui duyet]
    protected void lbtGuiduyet_Click(object sender, EventArgs e)
    {
        if (fckNoidungduyet.Value == "" || fckNoidungduyet.Value == "<br type=\"_moz\" />") { lblLoiguiduyet.Text = "Vui lòng nhập nội dung gửi duyệt !!"; return; } else { lblLoiguiduyet.Text = ""; }
        string img_path = "";
        if (fileupload_duyet.PostedFile != null && fileupload_duyet.PostedFile.ContentLength > 0)
        {
            img_path = System.IO.Path.GetFileName(fileupload_duyet.PostedFile.FileName);
            fileupload_duyet.PostedFile.SaveAs(Server.MapPath("Users/files/") + img_path.ToString().Trim());
            img_path = "Users/files/" + img_path.ToString().Trim();
        }
        tbReplyDATA ObjtbReplyDATA = new tbReplyDATA();
        ObjtbReplyDATA.prID = Session["ID"].ToString();
        ObjtbReplyDATA.reName = lblTieude.Text;
        ObjtbReplyDATA.reContent = fckNoidungduyet.Value;
        ObjtbReplyDATA.peFiles = img_path;
        ObjtbReplyDATA.uSend = uID;
        ObjtbReplyDATA.uRec = drlNguoinhan.SelectedValue;
        ObjtbReplyDATA.reType = "0";
        try
        {
            if (tbReplyDB.tbReply_Add(ObjtbReplyDATA))
            {
                lblThongbao.Text = "Gửi duyệt đề nghị thành công !!";
                Resetcontrol();
                pnDanhsach.Visible = true;
                pnUpdate.Visible = false;
                pnPhanhoi.Visible = false;
                pnDuyet.Visible = false;
            }
        }
        catch { lblLoiguiduyet.Text = "Lỗi dữ liệu nhập vào !!"; return; }
        Session["ID"] = null;
    }
    #endregion
    #region[Tro lai chi tiet]
    protected void lbtTrolaiduyet_Click(object sender, EventArgs e)
    {
        pnDuyet.Visible = false;
    }
    #endregion
    #region[ViewNameuSend]
    protected string NameUser(string uid)
    {
        string Chuoi = "";
        List<tbUsersDATA> list = tbUserDB.tbUsers_GetByID(uid);
        if (list.Count > 0)
        {
            Chuoi = list[0].uName;
        }
        list.Clear();
        list = null;
        return Chuoi;
    }
    #endregion
}
